package com.example.hw1.controller;

import com.example.hw1.entity.Lesson;
import com.example.hw1.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/Student")
public class helloController {
    private Student s;
    private List<Lesson> checkLesson;

    @GetMapping("/JumpStudent")
    public String addStudent(Model model){
        s=new Student();
        model.addAttribute("Student",s);
        return "PersonInformation";
    }

    @GetMapping("/Lesson")
    public String jumpLesson(@Validated @ModelAttribute("Student") Student student, Model model){
        s = new Student();
        s = student;
        //添加课程
        List<Lesson> lessons= Arrays.asList(
                new Lesson("高等数学"),
                new Lesson("Java基础"),
                new Lesson("阿水变成阿淼——成神之路"),
                new Lesson("论演员的自我修养"),
                new Lesson("计算机组成原理"),
                new Lesson("JavaWeb")
        );
        model.addAttribute("Student",s);
        model.addAttribute("lessons",lessons);
        return "LessonInformation";
    }
    @PostMapping("/Lesson")
    public String showTotalLesson(Model model, HttpServletRequest request){
        checkLesson = new ArrayList<>();
        String[] lessonsNums = request.getParameterValues("checkLessons");
        for (int i=0;i<lessonsNums.length;i++)
            checkLesson.add(new Lesson(lessonsNums[i]));
        s.setLessonList(checkLesson);

        model.addAttribute("Student",s);
        model.addAttribute("lessons",checkLesson);
        return "ShowLesson";
    }
}
